package kalambury.client;

import kalambury.KalamburyUtils;
import kalambury.srv.*;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.security.KeyStore;

/**
 * klasa implementuj�ca g��wne okno gry
 */
public class ClientForm extends javax.swing.JPanel {

    private static String serverAddress = "192.168.0.25";

    private SocketFactory sf;
    private Thread lt;
    private final ClientApplication app;

    /**
     * Creates new form ClientFor
     */
    public ClientForm(ClientApplication app) {
        this.app = app;
        System.out.println("clientform");
        try {
            char[] passphrase = "changeit".toCharArray();
            KeyStore ks = KeyStore.getInstance("JKS");
            InputStream in = this.getClass().getResourceAsStream("/.truststore");

            ks.load(in, passphrase);
            System.out.println("ks");
            TrustManagerFactory tms = TrustManagerFactory.getInstance("SunX509");
            tms.init(ks);

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tms.getTrustManagers(), null);

            sf = sslContext.getSocketFactory();
        } catch (Exception e) {
        }
        initComponents();

        ActionListener loginAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnLogInActionPerformed(e);
            }
        };
        btnLogIn.addActionListener(loginAction);
        txtName.addActionListener(loginAction);
        txtPass.addActionListener(loginAction);
    }

    // dodane tylko �eby gui designer mogl to narysowac
    private ClientForm() {
        this(null);
    }

    /**
     * ustawia adres serwera
     * @param adr adres serwera
     */
    public static void setServerAddress(String adr) {
        serverAddress = adr;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtPass = new JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        btnLogIn = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(250, 120));
        setMinimumSize(new java.awt.Dimension(250, 120));

        jLabel1.setText("Nazwa");

        jLabel2.setText("Hasło");

        btnLogIn.setText("Zaloguj");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1))
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnLogIn, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(btnLogIn)
                                .addContainerGap(22, Short.MAX_VALUE))
        );
    }// </editor-fold>

    // Variables declaration - do not modify
    private javax.swing.JButton btnLogIn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtName;
    private JPasswordField txtPass;
    // End of variables declaration

    private void btnLogInActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            GameConnection g = GameConnection.create(sf.createSocket(serverAddress, 1337), txtName.getText(), txtPass.getText());
            if (g == null) {
                JOptionPane.showMessageDialog(this, "Nieprawidłowe dane.");
                return;
            }
            app.setActivePanel(new GamePanel(app, g));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
